import Vue from 'vue';
import store from '@/store';
import { isString, isArray } from '@/utils/validate';
import settings from '@/settings';

const { errorLog } = settings;
// you can set in settings.js
// errorLog:'production' | ['production', 'development']
function checkNeed() {
  const env = import.meta.env.MODE;
  if (isString(errorLog)) {
    return env === errorLog;
  }
  if (isArray(errorLog)) {
    return errorLog.includes(env);
  }
  return false;
}

if (checkNeed()) {
  Vue.config.errorHandler = function (err, vm, info, a) {
    // Don't ask me why I use Vue.nextTick, it just a hack.
    // detail see https://forum.vuejs.org/t/dispatch-in-vue-config-errorhandler-has-some-problem/23500
    Vue.nextTick(() => {
      store.dispatch('errorLog/addErrorLog', {
        err,
        vm,
        info,
        url: window.location.href,
      });
      console.error(err, info);
    });
  };
}
